<?php
App::uses('AppController', 'Controller');
/**
 * Services Controller
 *
 * @property Service $Service
 */
class ServicesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Service->recursive = 0;
		$this->set('services', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Service->id = $id;
		if (!$this->Service->exists()) {
			throw new NotFoundException(__('Invalid service'));
		}
		$this->set('service', $this->Service->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($idCar = null) {
		if ($this->request->is('post')) {
			$this->Service->create();

			$data['Service']['numservicio'] = $this->request->data['Service']['numservicio'];
			$data['Service']['descripcion'] = $this->request->data['Service']['descripcion'];
			$data['Service']['fecha'] = date('Y-m-d', strtotime($this->request->data['Service']['fecha']));
			$data['Service']['kilometraje'] = $this->request->data['Service']['kilometraje'];
			$data['Service']['car_id'] = $this->request->data['Service']['car_id'];
			
			$data['Service']['desgloce'] = '';
			
			
			foreach ($this->request->data['Item'] as $key => $item) {
				$data['Service']['desgloce'] = $data['Service']['desgloce'].$item['concept'].';'.$item['price'].';';
			}
			
			if ($this->Service->save($data)) {
				$this->Session->setFlash(__('El servicio ha sido agregado correctamente!'), 'default', array(), 'good');
				$this->redirect(array('controller'=>'cars','action' => 'view',$this->request->data['Service']['car_id']));
			} else {
				$this->Session->setFlash(__('El servicio no ha sido agregado. Intente de nuevo.'), 'default', array(), 'bad');
			}
		}
		$cars = $this->Service->Car->find('list');
		$this->set(compact('cars'));
		if($idCar)
			$this->set('idCar',$idCar);
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Service->id = $id;
		if (!$this->Service->exists()) {
			throw new NotFoundException(__('Invalid service'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
						
			//$numItems = $this->request->data['Service']['cantItems'];
			
			$data['Service']['numservicio'] = $this->request->data['Service']['numservicio'];
			$data['Service']['descripcion'] = $this->request->data['Service']['descripcion'];
			$data['Service']['fecha'] = date('Y-m-d', strtotime($this->request->data['Service']['fecha']));
			$data['Service']['kilometraje'] = $this->request->data['Service']['kilometraje'];
			$data['Service']['car_id'] = $this->request->data['Service']['car_id'];
			$data['Service']['desgloce'] = '';
			
			foreach ($this->request->data['Item'] as $key => $item) {
				$data['Service']['desgloce'] = $data['Service']['desgloce'].$item['concept'].';'.$item['price'].';';
			}
			
			if ($this->Service->save($data)) {
				//$this->Session->setFlash(__('The service has been saved'));
				$this->Session->setFlash(__('Servicio guardado correctamente'), 'default', array(), 'good');
				//$this->redirect(array('action' => 'index'));
				$this->redirect(array('controller'=>'cars','action' => 'view',$this->request->data['Service']['car_id']));
			} else {
				$this->Session->setFlash(__('El servicio no ha sido guardado. Intente de nuevo'), 'default', array(), 'bad');
				
			}
		} else {
			$this->request->data = $this->Service->read(null, $id);
			$fecha = $this->request->data['Service']['fecha'];
			$this->request->data['Service']['fecha'] = date('d-m-Y', strtotime($fecha));
		}
		$cars = $this->Service->Car->find('list');
		$this->set(compact('cars'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		
		$this->Service->recursive = -1;
		
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Service->id = $id;
		if (!$this->Service->exists()) {
			throw new NotFoundException(__('Invalid service'));
		}
		$service = $this->Service->find('first',array('conditions'=>array('id'=>$id)));
		
		if ($this->Service->delete()) {
			$this->Session->setFlash(__('Servicio eliminado correctamente'), 'default', array(), 'good');
			$this->redirect(array('controller'=>'cars','action' => 'view',$service['Service']['car_id']));
		}
		$this->Session->setFlash(__('Servicio no pudo ser eliminado. Intente nuevamente'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
	
	
	public function search() {
		
		$numServicio = $this->request->data['Service']['numServicio'];
			
		$this->set('resultados',$this->Service->find('all',array('conditions'=>array('numservicio LIKE'=>$numServicio.'%'))));
		
		$this->render("search_results");
	} 
	
	
	public function imprimir($serviceId) {
		
		$this->layout = 'print';
		$this->Service->recursive = -1;
		$service = $this->Service->read(null, $serviceId);
		$this->set('service',$service['Service']);
		$this->Service->Car->recursive = -1;
		$car = $this->Service->Car->read(null,$service['Service']['car_id']);
		$this->set('car',$car['Car']);
		$this->Service->Car->Client->recursive = -1;
		$client = $this->Service->Car->Client->read(null,$car['Car']['client_id']);
		$this->set('client',$client['Client']);
	}
	
}
